/**
 * This file is part of aion-lightning <aion-lightning.org>
 *
 * aion-lightning is free software: you can redistribute it and/or modify it
 * under the terms of the GNU Lesser Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any
 * later version.
 *
 * aion-lightning is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License along with
 * aion-lightning. If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.clientpackets;

import com.aionengine.gameserver.model.EmotionType;
import com.aionengine.gameserver.model.actions.PlayerMode;
import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.gameobjects.state.CreatureState;
import com.aionengine.gameserver.model.templates.windstreams.WindstreamPath;
import com.aionengine.gameserver.network.aion.AionClientPacket;
import com.aionengine.gameserver.network.aion.AionConnection.State;
import com.aionengine.gameserver.network.aion.serverpackets.SM_EMOTION;
import com.aionengine.gameserver.network.aion.serverpackets.SM_WINDSTREAM;
import com.aionengine.gameserver.questEngine.QuestEngine;
import com.aionengine.gameserver.questEngine.model.QuestEnv;
import com.aionengine.gameserver.utils.PacketSendUtility;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class CM_WINDSTREAM extends AionClientPacket {

    private final Logger log = LoggerFactory.getLogger(CM_WINDSTREAM.class);
    int teleportId;
    int distance;
    int state;

    public CM_WINDSTREAM(int opcode, State state, State... restStates) {
        super(opcode, state, restStates);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void readImpl() {
        teleportId = readD();
        distance = readD();
        state = readD();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    protected void runImpl() {
        Player player = getConnection().getActivePlayer();
        switch (state) {
            case 0:
            case 4:
            case 7:
            case 8:
                if (state == 0) {
                    player.unsetPlayerMode(PlayerMode.RIDE);
                } else if (state == 7) { // start boost
                    PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, EmotionType.WINDSTREAM_START_BOOST, 0, 0), true);
                } else if (state == 8) { // end boost
                    PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, EmotionType.WINDSTREAM_END_BOOST, 0, 0), true);
                }
                PacketSendUtility.sendPacket(player, new SM_WINDSTREAM(state, 1));
                break;
            case 1:
                if (player.isInPlayerMode(PlayerMode.WINDSTREAM)) {
                    return;
                }
                if (player.isInState(CreatureState.GLIDING) || player.isInState(CreatureState.FLYING)) {
                    player.setPlayerMode(PlayerMode.WINDSTREAM, new WindstreamPath(teleportId, distance));
                    player.unsetState(CreatureState.ACTIVE);
                    player.unsetState(CreatureState.GLIDING);
                    player.setState(CreatureState.FLYING);
                    PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, EmotionType.WINDSTREAM, teleportId, distance), true);
                    player.getLifeStats().triggerFpRestore();
                    QuestEngine.getInstance().onEnterWindStream(new QuestEnv(null, player, 0, 0), teleportId);
                }
                break;
            case 2:
            case 3:
                if (!player.isInPlayerMode(PlayerMode.WINDSTREAM)) {
                    return;
                }
                player.unsetState(CreatureState.FLYING);
                player.setState(CreatureState.ACTIVE);
                if (state == 2) {
                    player.setState(CreatureState.GLIDING);
                    player.getLifeStats().triggerFpReduce();
                }
                PacketSendUtility.broadcastPacket(player, new SM_EMOTION(player, state == 2 ? EmotionType.WINDSTREAM_END
                        : EmotionType.WINDSTREAM_EXIT, 0, 0), true);
                player.getGameStats().updateStatsAndSpeedVisually();
                PacketSendUtility.sendPacket(player, new SM_WINDSTREAM(state, 1));
                player.unsetPlayerMode(PlayerMode.WINDSTREAM);
                break;
            default:
                log.error("Unknown Windstream state #" + state + " was found!");
        }
    }

}